package com.fc.remoting.logger;


import java.sql.Timestamp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONObject;
import com.fc.common.log.LoggingConfig;
import com.fc.monitor.client.Monitor;
import com.fc.monitor.client.message.EventProducer;


public class Monitorlogger {
	private Logger logger = LoggerFactory.getLogger(Monitorlogger.class);
	
	private Monitorlogger(){}
	
	static class LogFactory{
		private static Monitorlogger appLogger = new Monitorlogger();
	}
	
	public static Monitorlogger instance(){
		return LogFactory.appLogger;
	}
	
	public Logger getLogger(){
		return logger;
	}
	
	public static void monitorLog(String app,String module,String controllor,Object... objs){
//		monitorByLog(app,module,controllor, objs);
		
		monitorActivity(app,module,controllor, objs);
	}
	
	public static void monitorActivity(String app,String module,String controllor,Object... objs){
		
		
		Object exception = objs[0];
		Object request = objs[1];
		Object response = objs[2];
		Object other = null;
		
		if(objs.length > 3){
			other = objs[4];
		}
		
		StringBuilder sb = new StringBuilder();
		
		
		if(exception != null){
			String exceptionStr = LogSystem.getStackTrance((Throwable)exception);
			sb.append("exception:" + exceptionStr);
		}
		
		if(request != null){
			if(request instanceof JSONObject){
				sb.append("request:" + ((JSONObject)request).toJSONString());
			}else{
				sb.append("request:" + request.toString());
			}
		}
		
		if(response != null){
			if(response instanceof JSONObject){
				sb.append("response:" + ((JSONObject)response).toJSONString());
			}else{
				sb.append("response:" + response.toString());
			}
		}
		if(other != null)
			sb.append("info:" + other);
		
		EventProducer producer = Monitor.getProducer();
		producer.logEvent(app+"*"+module, app+"*"+module+"*"+controllor+"*ERROR", 1005, sb.toString(), "",
		new Timestamp(System.currentTimeMillis()));
		
//		producer.logEvent("fcpush*exception", "fcpush*exception*ALLException*ERROR", 1005, sb.toString(), "",
//				new Timestamp(System.currentTimeMillis()));
		
	}
	
	
    public static void monitorByLog(String app,String module,String controllor,Object... objs){
		
		Logger log = Monitorlogger.instance().getLogger();
		log.info(LoggingConfig.START_TAG);
		log.info(LoggingConfig.CONTROLLER_TAG + module + "." + module);
		
		log.info(LoggingConfig.OPERATION_TAG + controllor);
		//标记用户请求IP
		log.info(LoggingConfig.CLIENT_IP_TAG+"");
		//标记用户请求URL
		log.info(LoggingConfig.REQUEST_URI_TAG+"");
		//标记用户请求参数
		log.info(LoggingConfig.REQUEST_PARAMS_TAG+"");
		
		
		
		
		Object exception = objs[0];
		Object request = objs[1];
		Object response = objs[2];
		Object other = null;
		
		if(objs.length > 3){
			other = objs[4];
		}
		
		StringBuilder sb = new StringBuilder();
		
		
		if(exception != null){
			String exceptionStr = LogSystem.getStackTrance((Throwable)exception);
			sb.append("exception:" + exceptionStr);
		}
		
		if(request != null){
			if(request instanceof JSONObject){
				sb.append("request:" + ((JSONObject)request).toJSONString());
			}else{
				sb.append("request:" + request.toString());
			}
		}
		
		if(response != null){
			if(response instanceof JSONObject){
				sb.append("response:" + ((JSONObject)response).toJSONString());
			}else{
				sb.append("response:" + response.toString());
			}
		}
		if(other != null)
			sb.append("info:" + other);
		log.error(sb.toString());
		
		log.info(LoggingConfig.END_TAG);
		
    }	
	

}
